import {queryLogistics} from '../../api/api.js'
import {findMerchant,getOrderDetailByOrderId} from '../../api/api.js'





Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderDetail:{},
      phone:'',
      show:false,
      logis:"",
      logisFlag:true//表示是不是需要显示物流信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
     let currentPages = getCurrentPages();
     let currentPage = currentPages[currentPages.length-1];
     let options = currentPage.options;
     if(options.flag == "chat"){
         getOrderDetailByOrderId(options.id).then(res=>{
              console.log(res,"获取到一个订单============");
              let orderDetail = res.data.data;
              this.setData({
                orderDetail:orderDetail
              })

              this.getPhone(orderDetail.merchantId);
     
              // //查询物流信息
              this.queryLogistics();
         })

     }else{
        let orderDetail = JSON.parse(options.orderDetail);
        console.log("orderDetail数据",orderDetail);
        // 把地址和电话号码解析开
        orderDetail=  this.parseData(orderDetail);
        this.setData({
            orderDetail:orderDetail
        })

       
     }

  

  
  },


  // 重新解析传递过来的数据
  parseData(data){
     // 把地址字段取出来
     let address = data.address;
     //根据 - 分成两段
      let array = address.split("-");  
      console.log(array);
      // 取出最后一个，
      let newAddress = address.replace(array[array.length-1],'');
      let phone = array[array.length-1];
      console.log(newAddress,"-----",phone)
      data.address = newAddress;
      data.phone = phone;
      return data;
        
  },

  toLogis(){
      console.log("查看物流信息")
      let p = this.data.orderDetail.phone
      p = p.substring(p.length,p.length-4);
      let data = {
        sn:this.data.orderDetail.shippingCode,
        code:this.data.orderDetail.shippingName,
        phone:p
      }


      console.log(data,"---------------------查看物流参数-");

      data = JSON.stringify(data);

      wx.navigateTo({
        url:'/pages/logisDetail/logisDetail?data='+data,
      })
  },

   
  //跳转到toAfterSale 页面
  toAfterSale(afterSale){
      console.log("这个是父组件的方法",afterSale.detail);
      let afterSaleType = afterSale.detail;
      let orderDetail = this.data.orderDetail;
      orderDetail.afterSaleType = afterSaleType;
      this.setData({
          show:false
      })


      //获取子组件data 中保存的id
      let id = this.selectComponent("#com").data.goodId;
      console.log("选中的规格的id",id,orderDetail.details);
      // 根据id，过滤details 中的商品
      // let details = orderDetail.details;
      // console.log(details)
      // orderDetail.details = details.filter(item=>{
      //     return item.id == id;
      // })
      
      orderDetail = JSON.stringify(orderDetail);

      wx.navigateTo({
        url: '/pages/applyForAfterSales/applyForAfterSales?orderDetail='+orderDetail,
      }) 

  },

  //拨打电话的方法  
  makePhone(){
     if(this.data.phone){
        wx.makePhoneCall({
          phoneNumber:this.data.phone
        })
     }else{
       wx.showToast({
         title:'没有添加电话号码哦！',
         icon:"none"
       })
     }
    
    
  },

  //获取电话号码
  getPhone(merchantId){
    console.log(merchantId,"电话号码")
    
    findMerchant(merchantId).then(res=>{
         console.log(res,"商家电话号码");
         this.setData({
             phone:res.data.data.phone
         })
    })
  },


   //获取物流信息
    // sn(物流单号),  code(物流公司编码)
    queryLogistics(){
        // 判断订单的类型
        // 只有已经发货了的才可以查看订单
        // 待收货，待评价，售后中
        let orderDetail = this.data.orderDetail;
        // <!-- 0待付款/1待发货/2待收货/3待评价/4交易成功/5交易关 -->
        if(orderDetail.status == 0||orderDetail.status==1||orderDetail.status == 5){
           console.log("不需要获取物流信息======================")
           this.setData({
               logisFlag:false
           })
        }else{
           let p = this.data.orderDetail.phone
           p = p.substring(p.length,p.length-4);
            let data = {
              sn:this.data.orderDetail.shippingCode,
              code:this.data.orderDetail.shippingName,
              phone:p
              // sn:'773029789361554',
              // code:''
            }
            console.log(data,"-查看物流-数据------------");
            queryLogistics(data).then(res=>{
                console.log(res,"获取物流信息");
                if(res.data.code == 200){
                     if(res.data.data == null||res.data.data == ""){
                      this.setData({
                        logis:"没有获取到物流信息！"
                       })
                     }else if(res.data.data.status == 1){
                        this.setData({
                          logis:res.data.data.data[0].context
                        })
                     }else{
                        this.setData({
                            logis:"物流信息有误，请联系卖家修改"
                        })
                     }
                }else{
                    this.setData({
                      logis:"服务器错误，无法查询物流信息"
                    })
                }
            })
        }

    },



    // 点击申请售后面板的按钮
    afterSale(e){
      console.log("申请售后",e.currentTarget.dataset.idx);
      let idx = e.currentTarget.dataset.idx;
      if(idx == 0){
      //退款

      }else if(idx == 1){
        //换货
      }else if(idx == 2){
        // 退款退货
      }

      wx.navigateTo({
        url:'/pages/applyForAfterSales/applyForAfterSales',
      })
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})